var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');
var expressJwt = require('express-jwt');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var usersZiFu = require('./routes/zhifu');
var yjrRouter = require('./routes/yjr_index');
var FileRouter = require('./routes/FileApi');
var app = express();
app.use(cors())
 
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/zifu', usersZiFu);
app.use('/yjr', yjrRouter);
app.use('/api', FileRouter);


// 配置登录认证的中间件
app.use(expressJwt({
  secret: '123',//生成token时的口令
  algorithms: ['HS256']//解析token时的算法 ['HS256']
}).unless({
  path: [
    "/loginS",
    "/svgCode",
    "/updateId",
    "/api/chat",
    "/fu_user",
    "/api/add",
    "/api/verify",
    "/api/upload",
    "/api/merge",
    "/api/one",
    "/api/all",
    "/yjr/shopList",
    "/yjr/detail",
    "/yjr/static",
    "/yjr/shopAdd",
    "/yjr/addrAdd",
    "/yjr/addrList",
    "/yjr/shopTypeAdd",
    "/yjr/shopTypeList",
    "/yjr/placeAdd",
    "/yjr/placeList",
    "/yjr/notesAdd",
    "/yjr/notesList",
    "/yjr/commentAdd",
    "/yjr/commentList",
    "/yjr/editDetail",
    "/yjr/addrUpdate",
    "/yjr/detailStatic",
    "/yjr/noteList",
    "/yjr/addrDel",
    "/yjr/shopDetail",
    "/zifu/pcpay",
    "/loginREN",
    "/faceLogin",
    "/jwh/historical",
    "/jwh/getHistorical",
    "/addLogin",
    "/wlist",
    '/wshop'
  ]
  //白名单: 不需要进行token认证的路由都写在白名单中， 一般一个项目中 登录注册上传功能的接口不需要进行token认证
  //注意: 白名单的路由一定要书写完整 axios怎么访问这里就怎么写 axios.get("/user/login") 不要丢掉一级路由
}))

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  console.log(err);

  // res.locals.message = err.message;
  // res.locals.error = req.app.get('env') === 'development' ? err : {};

  // // render the error page
  // res.status(err.status || 500);
  // res.render('error');
});

module.exports = app;
